/*
* sc_s.c
*
* Created: 2012-07-05 20:06:45
*  Author: Antrykot
*/

#include "main.h"
#include "lcd.h"
#include "sc-s.h"
#include "utils.h"
#include "trans-s.h"
#include <stdlib.h>
#include "../sc-k/SWLcd.h"
#include <avr/sleep.h> 
#include <avr/power.h>

u8 kom_flag = 0;

ISR(SIG_INTERRUPT0)
{
	sleep_disable(); 
	TR_K_DISABLE;
	LG_ON;
	trans_k();
	WAITMS(4);
	LG_OFF;
	EIFR |= (1 << INTF0);
	TR_K_ENABLE;
	LG_OFF;
	kom_flag = 1;
}

ISR(SIG_OVERFLOW1)
{
	sleep_disable();
	TimerH++;
	u16 time = TimerH * 64 + (TCNT1 / 1000);
	
	if ((!PLU) || (!MIN) || (!ENT) )
	{
		last_time = time;
	}
 
}

ISR(SIG_OVERFLOW0)
{
	sleep_disable();
	t0_cnt ++;
	
	if (t0_cnt > 15)	// 30ms (256us * 8div * 15)
	{
		t0_cnt = 0;
		timeout = 1;
		T0STOP;
	}
}

ISR(SIG_OUTPUT_COMPARE2A)		// wake me up
{
	sleep_disable();
	time_cnt++;
}

int main(void)
{
	setup_io();
	
	ALARM1_OFF;
	ALARM2_OFF;

	LED_A_ON;
	LED_W_ON;
	
	AWP_SET_POWERFLAG;
	
	napa = 0;
	napb = 0;
	
	DAC_VCC_OFF;
	
	setup_int();
	_delay_ms(100);
	lcd_init();
	init_eeprom();


	TR_K_ENABLE;

	sei();
	intro();

	

#ifndef MEDIA_HCUR		// define MEDIAHCUR IN CH4 and CO2
	if (Ksp == 2)
	{
		CUR_SW_LOW;
	}
	else
	{
		CUR_SW_HIGH;
	}
#else
	CUR_SW_HIGH;
#endif

	
	awp_flags = 0;
	
	if (Pro == PRO_SERWIS)
	{
		pin_ok = 0;
		ref_pin = 2512;
		while(!pin_ok)
		{
			pin_loop();
			if (pin_ok == 1)
			{
				lcd_home();
				lcd_print(" SERWIS ");
				WAITMS(1000);
				Pro = PRO_SERWIS;
				
				u16 work_time = read_word(EEP_WORK_TIME);
				char txt[9];
				itoa(work_time,txt,10);
				lcd_home();
				lcd_print("WORK T: ");
				WAITMS(1000);
				lcd_home();
				
				for(u8 c = 0; c < 7 - strlen(txt); c++)
					lcd_sign(' ');
				
				lcd_print(txt);
				lcd_print("h");
				WAITMS(4000);
			}
			if (pin_ok == 255)
			{
				lcd_home();
				lcd_print("  BRAK  ");
				WAITMS(1000);
				lcd_print("ZEZWOLEN");
				WAITMS(1000);
				Pro = PRO_WORK;
			}
			WAITMS(100);
		}
	}
	
	while(1)
	{
		if (kom_flag)
		{
			/*
			u8 ok = trans_p();
			if (ok)
			{

			}
			else
			{
				LR_ON;
			}
			*/
		}
		
		check_keyboard();
		if (!PLU || !MIN || !ENT)
		{
			static u8 init_cnt = 0;
			init_cnt++;
			if (init_cnt > 10)
			{
				lcd_init();
				WAITMS(1);
				lcd_home();
				init_cnt = 0;
			}
		}

		
		//WAITMS(100);
		//trans_p();
		/*
		lcd_clear();
		char txt[16];
		itoa(rk_data,txt,10);
		lcd_print(txt);


		itoa(rk_addr,txt,10);
		lcd_print("/");
		lcd_print(txt);
		lcd_print("-----");
		*/
		work();
		
		if (tk)
		{
			//LR_ON;
			tk = 0;
			interpret_sk();
			//LR_OFF;
		}
		
		put_to_sleep();
		
	}
}

void put_to_sleep()
{
	set_sleep_mode(SLEEP_MODE_IDLE);
	sleep_enable();
	sleep_mode();
}


u16 Nr;
u8 Ksp;
u8 Awp;
u16 AL1;
u16 AL2;
u8 Kcl;

u16 U1_21;
u16 U1_20;
u16 U1_40;
u16 U1_35;

u16 U2_21;
u16 U2_20;
u16 U2_40;
u16 U2_35;

u8 uout;
u8 lcdmode;
u16 wk_zas;
u16 wk_aku;
u16 stw1;
u16 stw2;
u16 stn;

u8 serwis_flag;

u8 Pro;

u8 pin_ok = 0;
u16 ref_pin = 0;

u8 timeout = 0;
u8 t0_cnt = 0;

u16 pr_range_l = 9000;
u16 pr_range_h = 11000;

u8 param_trans = 0;

i16 corr_t = 0;
i16 corr_rh = 0;


char cuCharA[8] = {
	0b01110,
	0b10001,
	0b10001,
	0b10001,
	0b11111,
	0b10001,
	0b10001,
	0b00010
};

char cuCharL[8] = {
	0b10000,
	0b10010,
	0b10100,
	0b11000,
	0b10000,
	0b10000,
	0b11111,
	0b00000
};


